package com.micro.ai.models.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.models.entity.TrainingCheckpoint;
import com.micro.ai.models.service.TrainingCheckpointService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 训练检查点控制器
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/training-checkpoints")
@Tag(name = "训练检查点管理", description = "训练检查点的增删改查")
public class TrainingCheckpointController {

    @Autowired
    private TrainingCheckpointService checkpointService;

    @PostMapping
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('model:checkpoint:create')")
    @Operation(summary = "创建检查点", description = "创建训练检查点")
    public ApiResponse<TrainingCheckpoint> create(@Valid @RequestBody TrainingCheckpoint checkpoint) {
        log.info("创建检查点请求: jobId={}, checkpointName={}", 
                checkpoint.getJobId(), checkpoint.getCheckpointName());
        TrainingCheckpoint result = checkpointService.create(checkpoint);
        return ApiResponse.success("创建成功", result);
    }

    @GetMapping("/by-job/{jobId}")
    @PreAuthorize("hasRole('ADMIN') or hasRole('USER') or hasAuthority('model:checkpoint:view')")
    @Operation(summary = "获取任务的检查点列表", description = "根据任务ID获取所有检查点")
    public ApiResponse<Page<TrainingCheckpoint>> listByJobId(
            @Parameter(description = "任务ID", required = true) @PathVariable String jobId) {
        log.info("查询检查点列表: jobId={}", jobId);
        
        Page<TrainingCheckpoint> page = checkpointService.listByJobId(jobId);
        return ApiResponse.success(page);
    }

    @GetMapping("/best/{jobId}")
    @PreAuthorize("hasRole('ADMIN') or hasRole('USER') or hasAuthority('model:checkpoint:view')")
    @Operation(summary = "获取最佳检查点", description = "根据任务ID获取最佳检查点")
    public ApiResponse<TrainingCheckpoint> getBest(
            @Parameter(description = "任务ID", required = true) @PathVariable String jobId) {
        log.info("查询最佳检查点: jobId={}", jobId);
        
        TrainingCheckpoint checkpoint = checkpointService.getBest(jobId);
        if (checkpoint == null) {
            return ApiResponse.error("404", "未找到最佳检查点");
        }
        
        return ApiResponse.success(checkpoint);
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN') or hasRole('USER') or hasAuthority('model:checkpoint:view')")
    @Operation(summary = "获取检查点详情", description = "根据ID获取检查点详细信息")
    public ApiResponse<TrainingCheckpoint> getById(
            @Parameter(description = "检查点ID", required = true) @PathVariable String id) {
        log.info("查询检查点详情: checkpointId={}", id);
        
        TrainingCheckpoint checkpoint = checkpointService.getById(id);
        return ApiResponse.success(checkpoint);
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('model:checkpoint:update')")
    @Operation(summary = "更新检查点", description = "更新检查点信息")
    public ApiResponse<TrainingCheckpoint> update(
            @Parameter(description = "检查点ID", required = true) @PathVariable String id,
            @Valid @RequestBody TrainingCheckpoint checkpoint) {
        log.info("更新检查点请求: checkpointId={}", id);
        
        TrainingCheckpoint result = checkpointService.update(id, checkpoint);
        return ApiResponse.success("更新成功", result);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('model:checkpoint:delete')")
    @Operation(summary = "删除检查点", description = "删除检查点")
    public ApiResponse<Void> delete(
            @Parameter(description = "检查点ID", required = true) @PathVariable String id) {
        log.info("删除检查点请求: checkpointId={}", id);
        
        checkpointService.delete(id);
        return ApiResponse.successVoid("删除成功");
    }

}

